
Page({
  data: {
    id: null,
    todo: {},
    isEdit: false,
    content: "",
    description: ""
  },
  onLoad(query) {
    // console.log(getCurrentPages())
    const self = this
    this.setData({
      id: query.id
    })
    wx.request({
      url: `https://api.yizhanketang.cn/api/v1/todos/${self.data.id}`,
      success(res) {
        console.log(res.data.data)
        // self.data.todo = res.data.data
        self.setData({
          // todo: res.data.data
          content: res.data.data.content,
          description: res.data.data.description
        })
      }
    })
  },
  handleEdit() {
    const self = this
    if (!this.data.isEdit) {
      this.setData({
        isEdit: true
      })
      // console.log(this.data.isEdit)
    } else {
      // save
      // console.log(this.data.content)
      // console.log(this.data.description)
      // console.log("save")
      wx.request({
        method: "POST",
        url: `https://api.yizhanketang.cn/api/v1/todos/${self.data.id}`,
        data: {
          content: self.data.content,
          description: self.data.description
        },
        success(res) {
          self.setData({
            isEdit: false
          })
          wx.navigateBack({
            delta: -1
          })
        }
      })
    }
  }
})
